package com.function.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.function.domin.dto.GetCurrentWeatherDTO;
import com.function.domin.vo.AddressVO;
import com.function.domin.vo.CurrentWeatherVO;
import com.function.service.BaiduMapService;
import com.function.utils.BaiduMapUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Slf4j
@Service
public class BaiduMapServiceImpl implements BaiduMapService {

    @Autowired
    private BaiduMapUtils baiduMapUtils;

    /**
     * 获取地址
     * @return
     */
    public AddressVO getAddress() throws IOException {

        //查询本机网络ip
        String publicIp = baiduMapUtils.getPublicIp();
        log.info("当前网络ip：{}", publicIp);

        //获取当前地址的json格式数据
        JSONObject addressJson = baiduMapUtils.getAddress(publicIp);

        //用fastjson解析数据
        String addressString = addressJson.getString("address");

        //使用 split 方法，按竖线分隔字符串，并取第一个元素
        String country = addressString.split("\\|")[0];
        JSONObject detailAddressJson = (JSONObject) ((JSONObject) addressJson.get("content")).get("address_detail");
        String province = detailAddressJson.getString("province");
        String city = detailAddressJson.getString("city");

        //封装
        AddressVO addressVO = AddressVO.builder()
                .country(country)
                .province(province)
                .city(city)
                .build();

        return addressVO;
    }

    /**
     * 获取当前天气
     * @param getCurrentWeatherDTO
     * @return
     */
    public CurrentWeatherVO getCurrentWeather(GetCurrentWeatherDTO getCurrentWeatherDTO) throws IOException {

        //获取天气的json格式数据
        String districtId = getCurrentWeatherDTO.getDistrictId();
        JSONObject weatherJson = baiduMapUtils.getWeather(districtId);

        //用fastjson解析数据
        JSONObject locationJson = (JSONObject) ((JSONObject) (weatherJson.get("result"))).get("location");
        String country = locationJson.getString("country");
        String province= locationJson.getString("province");
        String city = locationJson.getString("city");
        String name = locationJson.getString("name");

        JSONObject nowJson = (JSONObject) ((JSONObject) (weatherJson.get("result"))).get("now");
        Integer temp = nowJson.getInteger("temp");
        Integer feelsLike = nowJson.getInteger("feels_like");
        String windClass = nowJson.getString("wind_class");
        String windDirection = nowJson.getString("wind_dir");
        String text = nowJson.getString("text");

        //封装为VO对象
        CurrentWeatherVO currentWeatherVO = CurrentWeatherVO.builder()
                .country(country)
                .province(province)
                .city(city)
                .name(name)
                .temp(temp)
                .feelsLike(feelsLike)
                .windClass(windClass)
                .windDirection(windDirection)
                .text(text)
                .build();
        log.info("当前天气：{}", currentWeatherVO);

        return currentWeatherVO;

    }

}
